package com.cby.operation.common.widget.richtext

import android.graphics.Paint.FontMetricsInt
import android.text.style.LineHeightSpan
import kotlin.math.roundToInt

/**
 * 本类用于去除TextView的自带行间距
 *
 * @author 徐智伟
 * @create 2020-03-27
 */
class ExcludeInnerLineSpaceSpan(
        /**
         * TextView行高
         */
        private val mHeight: Int) : LineHeightSpan {

    override fun chooseHeight(text: CharSequence, start: Int, end: Int,
                              spanstartv: Int, lineHeight: Int,
                              fm: FontMetricsInt) {
        // 原始行高
        val originHeight = fm.descent - fm.ascent
        if (originHeight <= 0) {
            return
        }
        // 计算比例值
        val ratio = mHeight * 1.0f / originHeight
        // 根据最新行高，修改descent
        fm.descent = (fm.descent * ratio).roundToInt()
        // 根据最新行高，修改ascent
        fm.ascent = fm.descent - mHeight
    }
}